<?php

class Crud {
    private $Sql;

    function __construct(){
         include 'conexao.php';
         $con = new conexao();
    }

    function pegarPrimaria($tabela) {
		// $keys = Array ();
		$queryChave = sprintf ( "SHOW KEYS FROM `%s`", $tabela );
		$resultChave = mysql_query ( $queryChave ) or die ( "Erro: " . mysql_error () );
		
		while ( $rowChave = mysql_fetch_assoc ( $resultChave ) ) {
			if ($rowChave ['Key_name'] == 'PRIMARY') {
				$chavePrimaria = $rowChave ['Column_name'];
			}
		}
		
		return $chavePrimaria;
	}
	
        public function AntiInjection($param) {
		$param = strip_tags ( $param ); // retirar as tags html
		$param = mysql_escape_string ( $param ); // Retirar todas tags referentes domysql ex: select, insert, update drop etc...
		return $param;
	}
        
	public function retornarCamposTabela($tabela) {
		$campos = array ();
		
		//$result = mysql_query ( "SHOW COLUMNS FROM " . $tabela ); // descobrir os campos da tabela
                $result = mysql_query ( "SHOW COLUMNS FROM " . $tabela );
		while ( $campo = mysql_fetch_assoc ($result)) {
			$campos [] = $campo ['Field'];
		}//unset ( $campos [0] ); // retirar o primeiro campo da lista
		return $campos;
	}

	function listar($tabela, $colunas, $nomeColunaTabela, $prefix, $codSql) {
            if($codSql==""){
                $sql = "select * from $tabela";
            }else{
                $sql = $codSql;
            }
            $lista = mysql_query($sql);
            //$dados = mysql_fetch_array($lista);
            //var_dump($dados);
            //$nomeColunaTabela = array_keys($dados);
            $colunas = explode(',', $colunas);
            $colunas = str_replace(' ', '', $colunas);
            $nomeColunaTabela = explode(',', $nomeColunaTabela);
            $nomeColunaTabela = str_replace(' ', '', $nomeColunaTabela);
             $totalDeCampos = count($colunas);
             /*   if($colunas=="" && $nomeColunaTabela==""){
                    //$colunas = $this->retornarCamposTabela($tabela);
                    $nomeColunaTabela = $colunas;
                    $totalDeCampos = count($colunas);//total de campos da tabela
                    $prefix ="";
                    
                }else{
                    $colunas = explode(',', $colunas);
                    $colunas = str_replace(' ', '', $colunas);
                    $totalDeCampos = count($colunas);
                    $nomeColunaTabela = explode(',', $nomeColunaTabela);
                    $nomeColunaTabela = str_replace(' ', '', $nomeColunaTabela);
                }
                */
                //$sql = "select * from $tabela";
                
                foreach ($nomeColunaTabela as $col){
                    echo '<th><div class=text-left field-sorting>';
                    echo "$col</th>";
                }
                echo "<th><div class=text-center field-sorting>Acao</th>
                      </tr>\n</thead>\n<tbody>";
		$w = 0;
                $zebra=0;
		while ( $linha = mysql_fetch_array($lista)){
                    if($zebra%2 == 0)
			echo "\n\t<tr class=erow>";
                    else
                        echo "\n\t<tr>";
                    $zebra++;
			for($w = 0; $w < $totalDeCampos; $w ++) {
                            echo "<td>";
			    echo $linha [$prefix.$colunas[$w]];
			    echo '</td>';
			}
                        echo '<td align=left width=5%>
                       <div class=tools>
                           <a href=http://localhost/teste/CodeIgniter/examples/index/delete.html/1 title="Delete Record" class="delete-row">
                               <span class="delete-icon"></span>
                               </a>
                           <a href= title=Edit Record class=edit_button><span class=edit-icon></span></a>
                           <a href= title=View Record class=edit_button><span class=read-icon></span></a>
                           <div class="clear"></div>
                        </div>
                      </td>';
			echo "</tr>";
		}
        }
	
	public function query($sql) {
		$q = mysql_query ( $sql );
		if ($q) {
			echo "<script>alert('Concluído');</script>";
		} else {
			echo "<script>alert('Erro: " . mysql_errno () . "');</script>";
		}
            }
            public function sqll($SQL){
                    $this->Sql = $SQL;
                }
} 
